package flc.ast.fragment;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;

import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import flc.ast.Content;
import flc.ast.R;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.adapter.AlbumAdapter1;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.utils.AppSpUtils;
import flc.ast.utils.CardLayoutManager;
import flc.ast.utils.CardLayoutManagerLan;
import flc.ast.utils.CardViewHelperCallback;
import flc.ast.utils.ItemTouchHelperCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.AppUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.view.container.StkEvent1Container;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private Dialog myAddAlbumDialog;
    private List<MyAlbumBean> listSp = new ArrayList<>();
    public static boolean isPub=true;
    private EditText dialogAdd;
    public static AlbumAdapter albumAdapter;
    private String[] SecurityStr;
    private int position = 0;//选择的密保问题角标
    private Dialog mySetPasswordDialog;
    private EditText dialogSetPass, dialogSetPassAgain, dialogSetAnswer;
    private Dialog myInputPasswordDialog;
    private EditText dialogInputPass;
    private Dialog myForgetPasswordDialog;
    private EditText dialogForgetAnswer, dialogForgetPass, dialogForgetPassAgain;
    private CardViewHelperCallback itemTouchHelperCallback=new CardViewHelperCallback();


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);
        mDataBinding.ivAddAlbum.setOnClickListener(this);
        mDataBinding.tvPriAlbum.setOnClickListener(this);
        mDataBinding.tvPubAlbum.setOnClickListener(this);
        mDataBinding.rvAlbumList.setLayoutManager(new CardLayoutManagerLan(mDataBinding.rvAlbumList));
        //设置密码弹框
        setPasswordDialog();
        //输入密码弹框
        inputPasswordDialog();
        //修改密码弹框
        forgetPasswordDialog();
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivAddAlbum:
                //添加相册弹框
                addAlbumDialog();
                myAddAlbumDialog.show();
                break;
            case R.id.tvPubAlbum:
                clearSelection();
                isPub=true;
                getData();
                mDataBinding.tvPubAlbum.setBackgroundResource(R.drawable.xuanzhong6);
                mDataBinding.tvPubAlbum.setTextColor(Color.parseColor("#E6FFFFFF"));
                break;
            case R.id.tvPriAlbum:
                boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isHave) {
                    dialogInputPass.setText("");
                    myInputPasswordDialog.show();
                } else {
                    dialogSetAnswer.setHint(SecurityStr[position]);//默认密保
                    mySetPasswordDialog.show();
                }
                break;
            case  R.id.tvDialogAddAlbumConfirm:
                if (isPub){
                    addAlbum();
                }else {
                    addPriAlbum();
                }
                myAddAlbumDialog.dismiss();
                break;
            case R.id.tvDialogAddAlbumCancel:
                myAddAlbumDialog.dismiss();
                break;
                //设置密码弹框----------------------------------
            case R.id.ivDialogSetSwitch://切换密保问题
                if (position == 0) {
                    position++;
                } else if (position == 1) {
                    position++;
                } else if (position == 2) {
                    position = 0;
                }
                dialogSetAnswer.setHint(SecurityStr[position]);
                break;
            case R.id.tvDialogSetCancel://取消
                mySetPasswordDialog.dismiss();
                break;
            case R.id.tvDialogSetRight://确定
                if (TextUtils.isEmpty(dialogSetPass.getText().toString()) || dialogSetPass.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_input_6_password);//为空或者小于6
                } else if (TextUtils.isEmpty(dialogSetPassAgain.getText().toString()) || dialogSetPassAgain.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_right_new_password);//为空或者小于6
                } else if (!dialogSetPass.getText().toString().equals(dialogSetPassAgain.getText().toString())) {
                    ToastUtils.showShort(R.string.two_password_no_equest);//两次密码不一致
                } else if (TextUtils.isEmpty(dialogSetAnswer.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_password_answer);//密保答案为空
                } else {
                    SPUtil.putBoolean(mContext, Content.isHavePassword, true);
                    SPUtil.putString(mContext, Content.myPassword, dialogSetPass.getText().toString());
                    SPUtil.putString(mContext, Content.myProblem, SecurityStr[position]);
                    SPUtil.putString(mContext, Content.myAnswer, dialogSetAnswer.getText().toString());
                    ToastUtils.showShort(R.string.set_password_suc);
                    mySetPasswordDialog.dismiss();
                }
                break;
            //输入密码弹框----------------------------------
            case R.id.tvDialogInputCancel://取消
                myInputPasswordDialog.dismiss();
                break;
            case R.id.tvDialogInputRight://确定
                String password = SPUtil.getString(mContext, Content.myPassword, "");
                if (TextUtils.isEmpty(dialogInputPass.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_6_password);
                } else if (!dialogInputPass.getText().toString().equals(password)) {
                    ToastUtils.showShort(R.string.password_def);
                } else {
                    myInputPasswordDialog.dismiss();
                    clearSelection();
                    isPub=false;
                    getPriData();
                    mDataBinding.tvPriAlbum.setBackgroundResource(R.drawable.xuanzhong6);
                    mDataBinding.tvPriAlbum.setTextColor(Color.parseColor("#E6FFFFFF"));
                }
                break;
            case R.id.tvDialogInputForget://忘记密码
                myInputPasswordDialog.dismiss();
                dialogForgetAnswer.setText("");
                dialogForgetAnswer.setHint(SPUtil.getString(mContext, Content.myProblem, ""));
                dialogForgetPass.setText("");
                dialogForgetPassAgain.setText("");
                myForgetPasswordDialog.show();
                break;
            //忘记密码弹框----------------------------------
            case R.id.tvDialogForgetCancel://取消
                myForgetPasswordDialog.dismiss();
                break;
            case R.id.tvDialogForgetRight://确认
                if (TextUtils.isEmpty(dialogForgetAnswer.getText().toString())) {
                    ToastUtils.showShort(R.string.please_input_password_answer);
                } else if (TextUtils.isEmpty(dialogForgetPass.getText().toString()) || dialogForgetPass.getText().length() < 6) {
                    ToastUtils.showShort(R.string.please_input_6_password);
                } else if (TextUtils.isEmpty(dialogForgetPassAgain.getText().toString()) || dialogForgetPassAgain.getText().length() < 6) {
                    ToastUtils.showShort(R.string.again_input_6_password);
                } else if (!dialogForgetPass.getText().toString().equals(dialogForgetPassAgain.getText().toString())) {
                    ToastUtils.showShort(R.string.two_password_no_equest);
                } else if (!dialogForgetAnswer.getText().toString().equals(SPUtil.getString(mContext, Content.myAnswer, ""))) {
                    ToastUtils.showShort(R.string.answer_def);
                } else {
                    SPUtil.putString(mContext, Content.myPassword, dialogForgetPass.getText().toString());
                    ToastUtils.showShort(R.string.update_password_suc);
                    myForgetPasswordDialog.dismiss();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        //密保问题
        SecurityStr = new String[]{getString(R.string.problem1), getString(R.string.problem2), getString(R.string.problem3)};//密保问题
    }

    private void getData() {
        listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            mDataBinding.rvAlbumList.setLayoutManager(new CardLayoutManagerLan(mDataBinding.rvAlbumList));
            AlbumAdapter1 albumAdapter1=new AlbumAdapter1(mContext,listSp);
            albumAdapter1.setSelPosition(listSp.size()-1);
            mDataBinding.rvAlbumList.setAdapter(albumAdapter1);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llMyAlbumNoDate.setVisibility(View.GONE);
//            itemTouchHelperCallback = new CardViewHelperCallback(listSp, albumAdapter1);
            itemTouchHelperCallback.setmAdapter(albumAdapter1);
            itemTouchHelperCallback.setmDatas(listSp);
            ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
            itemTouchHelper.attachToRecyclerView(mDataBinding.rvAlbumList);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llMyAlbumNoDate.setVisibility(View.VISIBLE);
        }
    }

    private void addAlbum(){
        String albumName = dialogAdd.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, new ArrayList<>(),isPub,0));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
           AppSpUtils.saveAlbumList(listSp);
        } else {
            AppSpUtils.saveAlbumList(listSave);
        }
        getData();
    }

    private void getPriData() {
        listSp = AppSpUtils.getPriAlbumList();
        if (listSp != null && listSp.size() > 0) {
            mDataBinding.rvAlbumList.setLayoutManager(new CardLayoutManagerLan(mDataBinding.rvAlbumList));
            AlbumAdapter1 albumAdapter1=new AlbumAdapter1(mContext,listSp);
            albumAdapter1.setSelPosition(listSp.size()-1);
            mDataBinding.rvAlbumList.setAdapter(albumAdapter1);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.llMyAlbumNoDate.setVisibility(View.GONE);
//            itemTouchHelperCallback = new CardViewHelperCallback(listSp, albumAdapter1);
            itemTouchHelperCallback.setmAdapter(albumAdapter1);
            itemTouchHelperCallback.setmDatas(listSp);
            ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemTouchHelperCallback);
            itemTouchHelper.attachToRecyclerView(mDataBinding.rvAlbumList);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.llMyAlbumNoDate.setVisibility(View.VISIBLE);
        }
    }

    private void addPriAlbum(){
        String albumName = dialogAdd.getText().toString();
        if (TextUtils.isEmpty(albumName)) {
            ToastUtils.showShort(R.string.please_input_album_name);
            return;
        }
        myAddAlbumDialog.dismiss();
        //准备数据
        List<MyAlbumBean> listSave = new ArrayList<>();
        listSave.add(new MyAlbumBean(TimeUtils.getNowString(), albumName, new ArrayList<>(),isPub,0));
        //保存数据
        List<MyAlbumBean> listSp = AppSpUtils.getPriAlbumList();
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            AppSpUtils.savePriAlbumList(listSp);
        } else {
            AppSpUtils.savePriAlbumList(listSave);
        }
        getPriData();
    }

    private void clearSelection() {
        mDataBinding.tvPubAlbum.setBackground(null);
        mDataBinding.tvPubAlbum.setTextColor(Color.parseColor("#FF000106"));
        mDataBinding.tvPriAlbum.setBackground(null);
        mDataBinding.tvPriAlbum.setTextColor(Color.parseColor("#FF000106"));
    }

    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(true);
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogAddAlbumConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogAddAlbumCancel);
        dialogAdd = inflate.findViewById(R.id.etDialogAddAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //设置密码弹框
    private void setPasswordDialog() {
        mySetPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_set_password, null);
        mySetPasswordDialog.setContentView(inflate);
        mySetPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = mySetPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogClose = inflate.findViewById(R.id.tvDialogSetCancel);
        ImageView dialogSwitch = inflate.findViewById(R.id.ivDialogSetSwitch);
        TextView dialogCom = inflate.findViewById(R.id.tvDialogSetRight);
        dialogClose.setOnClickListener(this);
        dialogSwitch.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogSetPass = inflate.findViewById(R.id.etDialogSetPassword);
        dialogSetPassAgain = inflate.findViewById(R.id.etDialogSetAgain);
        dialogSetAnswer = inflate.findViewById(R.id.etDialogSetAnswer);
    }

    //输入密码弹框
    private void inputPasswordDialog() {
        myInputPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_input_password, null);
        myInputPasswordDialog.setContentView(inflate);
        myInputPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myInputPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogClose = inflate.findViewById(R.id.tvDialogInputCancel);
        TextView dialogForget = inflate.findViewById(R.id.tvDialogInputForget);
        TextView dialogCom = inflate.findViewById(R.id.tvDialogInputRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogForget.setOnClickListener(this);
        dialogInputPass = inflate.findViewById(R.id.etDialogInputPassword);
    }

    //修改密码弹框
    private void forgetPasswordDialog() {
        myForgetPasswordDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_forget_password, null);
        myForgetPasswordDialog.setContentView(inflate);
        myForgetPasswordDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myForgetPasswordDialog.getWindow();
        //设置Dialog从窗体中心弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.95);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView dialogClose = inflate.findViewById(R.id.tvDialogForgetCancel);
        TextView dialogCom = inflate.findViewById(R.id.tvDialogForgetRight);
        dialogClose.setOnClickListener(this);
        dialogCom.setOnClickListener(this);
        dialogForgetAnswer = inflate.findViewById(R.id.etDialogForgetAnswer);
        dialogForgetPass = inflate.findViewById(R.id.etDialogForgetPassword);
        dialogForgetPassAgain = inflate.findViewById(R.id.etDialogForgetAgain);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isPub){
            getData();
        }else{
            getPriData();
        }
    }
}